package com.bidianying.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.RowSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

@Repository
public class MaizuoDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public int updateMovieInfoForMaizuo(List<Map<String,String>> list){
		final List<Map<String,String>> tempList = list;   
		String sql = "update tMovie_info set maizuoMovieId=?,maizuoMovieName=? where id = ?";
		int[] resultInt = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps, int i)throws SQLException {
				ps.setString(1, tempList.get(i).get("maizuoMovieId"));
				ps.setString(2, tempList.get(i).get("maizuoMovieName"));
				ps.setString(3, tempList.get(i).get("id"));
			}
			@Override
			public int getBatchSize() {
				return tempList.size();
			}
		});
		return resultInt.length;
	}
	
	public int saveMaizuoCityInfo(List<Map<String,String>> list){
		final List<Map<String,String>> tempList = list;   
		String sql = "insert into tMaizuo_city(name,cityId,provinceId,pinyin,areaType) values(?,?,?,?,?)";
		int[] resultInt = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps, int i)throws SQLException {
				ps.setString(1, tempList.get(i).get("name"));
				ps.setString(2, tempList.get(i).get("cityId"));
				ps.setString(3, tempList.get(i).get("provinceId"));
				ps.setString(4, tempList.get(i).get("pinyin"));
				ps.setString(5, tempList.get(i).get("areaType"));
			}
			@Override
			public int getBatchSize() {
				return tempList.size();
			}
		});
		return resultInt.length;
	}
	
	public List<Map<String,String>> getAllMaizuoCity(){
		String sql = "select * from tMaizuo_city";
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		while(rs.next()){
			Map<String,String> map = new HashMap<String,String>();
			map.put("cityId", rs.getString("cityId"));
			map.put("pinyin", rs.getString("pinyin"));
			resultList.add(map);
		}
		return resultList;
	}
	
	public int saveMaizuoCinemaInfo(List<Map<String,String>> list){
		final List<Map<String,String>> tempList = list;   
		String sql = "insert into tMaizuo_cinema(cinemaId,address,businessCircle,cityId,regionName,tel,name) values(?,?,?,?,?,?,?)";
		int[] resultInt = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps, int i)throws SQLException {
				ps.setString(1, tempList.get(i).get("cinemaId"));
				ps.setString(2, tempList.get(i).get("address"));
				ps.setString(3, tempList.get(i).get("businessCircle"));
				ps.setString(4, tempList.get(i).get("cityId"));
				ps.setString(5, tempList.get(i).get("regionName"));
				ps.setString(6, tempList.get(i).get("tel"));
				ps.setString(7, tempList.get(i).get("name"));
			}
			@Override
			public int getBatchSize() {
				return tempList.size();
			}
		});
		return resultInt.length;
	}
	
}
